//
//  MainTabbarController.swift
//  WeiBo_One
//
//  Created by apple on 15/10/19.
//  Copyright © 2015年 蜡笔小炎. All rights reserved.
//

import UIKit


class MainTabbarController: UITabBarController {
    
    // MARK: - 懒加载控件
    lazy private var writeBtn: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")

    override func viewDidLoad() {
        super.viewDidLoad()
    
        //添加所有自控制器
        addChildViewControllers()
        //添加撰写按钮
        addWriteButton()
        
        
    }
    
    /// 添加撰写按钮
    func addWriteButton (){
    
        writeBtn.sizeToFit()
        writeBtn.center = tabBar.center
        view.addSubview(writeBtn)
        
        writeBtn.addTarget(self, action: "writeBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    /// 撰写按钮的点击事件
    @objc private func writeBtnDidClick (){
        print("点击了撰写按钮")
    }
    
    /// tabBarController 添加所需要的所有控制器
    func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), name: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), name: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), name: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), name: "我", imageName: "tabbar_profile")
    }
    
    /// tabBarController 添加单个控制器
    func addChildViewController (childController: UIViewController, name: String, imageName: String) {
        childController.title = name
        
        let controller = UINavigationController(rootViewController: childController)
        
        addChildViewController(controller)
        
        controller.tabBarItem.image = UIImage(named: imageName)
    }
}
